#include "common.h"
#include <QRegExp>
#include <QSettings>
#include <QRegExpValidator>
#include <QApplication>
#include <QStandardPaths>
#include <QFileInfo>
#include <QDebug>

namespace common {	
    const QString gRenameRegExp = "^[\u4e00-\u9fa5a-zA-Z0-9\\_]+$";
    const QString gPrevVersion = "XMultiTool_V";
    const QString gVersionSymbol = "v";
    QString gRunProgramName = "XMultiTool";

    bool checkNameRules(QString newName)
    {
        // 先匹配正则表达式
//        QRegExp regx(common::gRenameRegExp);
//        QValidator *validator = new QRegExpValidator(regx);
//        QString regName = newName;
//        regName.replace(" ", "");
//        newName = newName.trimmed();
//        int pos = 0;
//        if(validator->validate(regName, pos) != QValidator::Acceptable)
//            return false;

        return true;
    }

    QString genNewName(const QString& oldName, const QStringList& list)
    {
        QString newName = oldName;
        int index = 1;
        while(list.indexOf(newName) >= 0)
        {
            newName = oldName + QString("_%1").arg(index);
            index++;
        }

        return newName;
    }

    QByteArray encrypt(const QByteArray& data) {
        QByteArray encryData;
        encryData.resize(data.size());
        for (auto i = 0; i < data.size(); i++) {
            encryData[i] = data[i] + 5;
        }

        return encryData.toBase64();
    }

    QByteArray decrypt(const QByteArray& data) {
        QByteArray decryData = data.fromBase64(data);
        for (auto i = 0; i < decryData.size(); i++) {
            decryData[i] = decryData[i] - 5;
        }

        return decryData;
    }

    bool isInternationalVersion() {
        QString cfgPath = QCoreApplication::applicationDirPath() + "/global_config.ini";
        QSettings setting(cfgPath, QSettings::IniFormat);
        // serverTag国内和国外区分按本地的配置不继承
        setting.beginGroup(CLOUD_GROUP);
        return (setting.value(KEY_SERVERTAG).toInt() == 0) ? true : false;
    }

    const QString getApplicationName() {
        if(isInternationalVersion())
            return QString("Crash Reporter");
        else
            return QString("崩溃报告");
    }

    const QString curVersionRemoveV() {
        return QApplication::applicationVersion().remove("v");
    }

    const std::string converseToUtf8(const QString& converseString) {
#ifdef Q_OS_WIN
        bool use_utf8_encode = true;
#else
        bool use_utf8_encode = true;
#endif
        std::string dstString = std::string((const char *)converseString.toLocal8Bit().constData());
        if (use_utf8_encode){
            dstString = converseString.toUtf8().constData();
        }

        return dstString;
    }

    const QString converseToUtf8(const std::string& converseString) {
#ifdef Q_OS_WIN
        bool use_utf8_encode = true;
#else
        bool use_utf8_encode = true;
#endif
        QString dstString = QString::fromLocal8Bit(converseString.c_str());
        if (use_utf8_encode){
            dstString = QString::fromUtf8(converseString.c_str());
        }

        return dstString;
    }

    void writeRegister(const QString& regKey, const QVariant& value) {
#ifdef Q_OS_WIN
        QString regPath = "HKEY_CURRENT_USER\\Software\\" + QCoreApplication::organizationName();
#else
        QString regPath = QStandardPaths::writableLocation(QStandardPaths::ConfigLocation) + "/com." \
            + QApplication::organizationName() + "." + QApplication::applicationName() + ".plist";
//        QFileInfo info = QFileInfo(regPath);
//        if(!info.exists()){
//            settings.setValue(autoTest, "1");
//        }
#endif

        QSettings settings(regPath, QSettings::NativeFormat);
        settings.setValue(regKey, value);
        settings.sync();
    }

    QVariant readRegister( const QString& regKey, QVariant defaultValue ) {
#ifdef Q_OS_WIN
        QString regPath = "HKEY_CURRENT_USER\\Software\\" + QCoreApplication::organizationName();
#else
        QString regPath = QStandardPaths::writableLocation(QStandardPaths::ConfigLocation) + "/com." \
            + QApplication::organizationName() + "." + QApplication::applicationName() + ".plist";
#endif

        QSettings settings(regPath, QSettings::NativeFormat);
        return settings.value(regKey, defaultValue);
    }

    bool existRegister(const QString& regKey) {
#ifdef Q_OS_WIN
        QString regPath = "HKEY_CURRENT_USER\\Software\\" + QCoreApplication::organizationName();
#else
        QString regPath = QStandardPaths::writableLocation(QStandardPaths::ConfigLocation) + "/com." \
            + QApplication::organizationName() + "." + QApplication::applicationName() + ".plist";
#endif

        QSettings settings(regPath, QSettings::NativeFormat);
        return settings.allKeys().contains(regKey);
    }

    void debugJsonFormat(const QByteArray& data)
    {
#if DEBUG_JSON
        QFile json (QApplication::applicationDirPath() + "./debugjsonformat.json");
        if(json.open(QIODevice::Append | QIODevice::Text))
        {
            json.write("\n");
            json.write(data);
            json.write("\n");
            json.close();
        }
#endif
    }
}
